package org.brothers.showcase.demo.web.account;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.brothers.showcase.demo.entity.account.Group;
import org.brothers.showcase.demo.entity.account.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springside.modules.mapper.CollectionMapper;

@Controller
@RequestMapping(value = "/account/user")
public class UserController {

//	private AccountManager accountManager;
	

	@RequestMapping(value = { "list", "" })
	public String list() {
		return "account/userList";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable Long id, Model model) {
//		User user = accountManager.getUser(id);
		User user = new User();
		user.setId(id);
		user.setName("Martin");
		model.addAttribute(user);
		return "account/userForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute(new User());
		return "account/userForm";
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String saveForm(Model model) {
		model.addAttribute(model);
		return "account/userForm";
	}

	@ModelAttribute("groups")
	public Map<Long, String> getGroups() {
//		List<Group> groupList = accountManager.getAllGroup();
		List<Group> groupList = new ArrayList<Group>();
//		Group group1 = new Group(1L,"group1");
//		Group group2 = new Group(2L,"group2");
//		Collections.addAll(groupList, group1,group2);
		return CollectionMapper.extractToMap(groupList, "id", "name");
	}

	/*@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}*/
}
